﻿@model IEnumerable<WidgetTemplate>
@{
    Layout = "~/Views/Shared/_Dashboard.cshtml";
    var _dbContext = WebSiteContext.Current.DataContext;
    var _category = ViewBag.Category;
    int conflicts = ViewBag.Conflicts;
}
<style>
    .d-list-item:hover { background-color: #f9fdec; }
    .hr { margin: 0px; }
</style>
@UIHelper.ImageTitle(Html.Global("WidgetMan"), Html.Global("WidgetMan_Desc"), "~/content/images/icon_widgets_48.png")
<table>
    <tr>
        <td>
            @Html.Global("RES_CATEGORY"):
        </td>
        <td>@(Ajax.Dna().ComboBox()
        .Bind(_dbContext.WidgetTemplates.GetCategories())
        .Options(opts =>
        {
            opts.OnItemSelected = "location=\"" + Url.Action("WidgetManager", "Widget") + "?category=\"+ui.value;";
        })
        .DropDownList()
        .Select(_category)
        .GetHtml())
        </td>
        <td>
            @Ajax.Dna().Button().LocText("RES_DETECT").ImageIcons(Url.Content("~/content/images/icon_search_page_16.png")).Click("_detectWidgets();").GetHtml()
        </td>
        @if ((conflicts > 0) && WebSiteContext.Current.IsAuthorized<DNA.Mvc.Controllers.WidgetController>("WidgetManager"))
        {
            <td>
                @using (Html.BeginForm("ResolveConflict", "Widget", FormMethod.Post))
                {
                    @Ajax.Dna().Button().LocText("RES_RESOLVE").ImageIcons(Url.Content("~/content/images/icon_refresh_16.png")).Submit().GetHtml()
                }
            </td>
        }
        <td>@Ajax.Dna().Link(Html.Global("RES_HELP"), Url.Help(6), Url.Content("~/content/images/help.png")).Options(opts => { opts.Target = "_blank"; }).GetHtml()</td>
    </tr>
</table>
@if (conflicts > 0)
{
    @UIHelper.SectionHeader(Html.Global("RES_CONFLICTS") + " ( " + conflicts.ToString() + " )")
}
else
{
    <hr />
}
@foreach (var tmpl in Model)
{
    var usingCount = _dbContext.WidgetDescriptors.InusingWidgetsCount(tmpl.ID);
    var isInstalled = _dbContext.WidgetDescriptors.Contains(w => w.InstalledPath.Equals(tmpl.ID, StringComparison.OrdinalIgnoreCase));
    var _descriptor = _dbContext.WidgetDescriptors.Find(tmpl.ID);
    var isVerConflict = false;
    var isModifiedConflict = false;
    var tmplVersion = "";
    var descVersion = "";
    if (isInstalled)
    {
        tmplVersion = string.IsNullOrEmpty(tmpl.Version) ? "Unknow" : tmpl.Version;
        descVersion = string.IsNullOrEmpty(_descriptor.Version) ? "Unknow" : _descriptor.Version;
        var tmplModified = tmpl.Modified;
        var descModified = _descriptor.Modified;

        if (!tmplVersion.ToLower().Equals(descVersion.ToLower()))
        {
            isVerConflict = true;
        }

        if ((tmplModified - descModified).Seconds != 0)
        {
            isModifiedConflict = true;
        }
    }

    var _icon = "";
    if ((tmpl.Icons != null) && (tmpl.Icons.Count > 0))
    {
        var ico = tmpl.Icons.FirstOrDefault(i => i.Width >= 36);

        if (!string.IsNullOrEmpty(ico.Source))
        {
            _icon = ico.Source;
        }
    }
    var iconUrl = !string.IsNullOrEmpty(_icon) ? Url.Content("~/content/widgets/" + tmpl.ID.Replace("\\", "/") + "/" + _icon) : Url.Content("~/content/images/app.gif");
    <div class="d-list-item">
        <table style="width: 100%;">
            <tr>
                <td style="width: 60px;">
                    <img alt="@tmpl.Name" src="@iconUrl" />
                </td>
                <td style="vertical-align: top; white-space: normal;">
                    <h3 style="margin: 5px 0px;">@tmpl.Defaults.Title</h3>
                    <p>
                        @tmpl.Description
                    </p>
                    <table>
                        <tr>
                            <td>
                                @(
 Ajax.Dna().Link("javascript:void(0);")
                        .Text(isInstalled ? Html.Global("RES_INSTALLED") : Html.Global("RES_PENDING"))
                        .ImageIcons(Url.Content(isInstalled ? "~/content/images/icon_accept_16.png" : "~/content/images/icon_clock_16.png"))
                        .GetHtml()
                        )
                            </td>
                            <td>
                                @if (isInstalled)
                                {
                                    @(Ajax.Dna().Link("javascript:void(0);")
                            .Text(Html.Global("RES_USING") + "(" + usingCount.ToString() + ") ")
                            .ImageIcons(Url.Content("~/content/images/icon_add_content_16.gif"))
                            .GetHtml())
                                }
                            </td>
                            @if (!string.IsNullOrEmpty(tmpl.Author.Name))
                            {
                                <td>@Ajax.Dna().Link("javascript:void(0);").ImageIcons(Url.Content("~/content/images/icon_user_16.png")).LocText("RES_AUTHOR").GetHtml():
                                </td>
                                <td>
                                    @Ajax.Dna().Link(string.IsNullOrEmpty(tmpl.Author.Email) ? "javascript:void(0);" : "mailto:" + tmpl.Author.Email).Text(tmpl.Author.Name).GetHtml();
                                </td>
                            }
                            @if (!string.IsNullOrEmpty(tmpl.Author.Uri))
                            {
                                <td>@Ajax.Dna().Link("javascript:void(0);").ImageIcons(Url.Content("~/content/images/icon_home_16.png")).LocText("RES_ORGANIZATION").GetHtml()
                                </td>
                                <td>@tmpl.Author.Uri
                                </td>
                            }
                            <td>@Html.Global("RES_VERSION"):
                            </td>
                            <td>
                                @tmpl.Version
                            </td>
                            <td>
                                Modified:
                            </td>
                            <td>
                                @Html.Global(tmpl.Modified)
                            </td>
                            @if ((tmpl.Licenses != null) && (tmpl.Licenses.Count > 0))
                            {
                                <td>@Html.Global("RES_LICENSES"):
                                </td>
                                <td>
                                    @foreach (var lic in tmpl.Licenses)
                                    {
                                        <a target="_blank" href="@lic.Source" >@lic.Text</a>
                                    }
                                </td>
                            }
                        </tr>
                        @if (isVerConflict || isModifiedConflict)
                        { 
                            <tr>
                                <td>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                                <td>
                                    @if (isVerConflict)
                                    { 
                                        <span style="color: Red;">Conflict:</span>
                                    }
                                </td>
                                <td>
                                    @if (isVerConflict)
                                    {
                                        <div class="ui-state-error" style="padding: 5px; display: inline-block;">
                                            @descVersion
                                        </div>
                                    }
                                </td>
                                <td>
                                    @if (isModifiedConflict)
                                    { 
                                        <span style="color: Red;">Conflict</span>
                                    }
                                </td>
                                <td>
                                    @if (isModifiedConflict)
                                    {
                                        <div class="ui-state-error" style="padding: 5px; display: inline-block;">
                                            @_descriptor.Modified.ToString()
                                        </div>
                                    }
                                </td>
                            </tr>
                        }
                    </table>
                </td>
                <td style="width: 100px;">
                    <div>
                        @if (string.IsNullOrEmpty(tmpl.Defaults.Controller) && string.IsNullOrEmpty(tmpl.Defaults.Action))
                        {
                            Ajax.Dna().Button("btnRepublish_" + tmpl.Name)
                               .LocText("RES_EXPORT")
                               .Click("window.open(\"" + Url.Action("PackWidget", "Widget", new { Area = "", wid = Server.UrlEncode(tmpl.ID) }) + "\");")
                               .ImageIcons(Url.Content("~/content/images/icon_download_db_16.png"))
                               .Render();
                        }
                    </div>
                    @if (!isInstalled)
                    {
                        <div>
                            @{Ajax.Dna().Button("btnRegister_" + tmpl.Name)
                                  .LocText("RES_REGISTER")
                                  .Click("_registerWidgetDescriptor(\"" + Server.UrlEncode(tmpl.ID) + "\");")
                                  .ImageIcons(Url.Content("~/content/images/icon_process_16.png"))
                                  .Render();                        }
                        </div>
                        <div>
                            @{Ajax.Dna().Button("btnDelete_" + tmpl.Name)
                                .LocText("RES_DELETE")
                                .Click("_deleteWidgetDescriptor(\"" + Server.UrlEncode(tmpl.ID) + "\");")
                                .ImageIcons(Url.Content("~/content/images/icon_delete_16.png"))
                                .Render();                        }
                        </div>
                    }
                    else
                    {

                        if (usingCount == 0)
                        {
                        <div>
                            @{Ajax.Dna().Button("btnUnRegister_" + tmpl.Name)
                                .LocText("RES_UNREGISTER")
                                .Click("_unRegisterWidgetDescriptor(\"" + Server.UrlEncode(tmpl.ID) + "\");")
                                .ImageIcons(Url.Content("~/content/images/icon_clock_16.png"))
                                .Render();}
                        </div>
                        }
                    }
                    <div>
                        @if (!string.IsNullOrEmpty(tmpl.Author.Email) && (tmpl.Author.Email.Equals(Membership.GetUser(User.Identity.Name).Email, StringComparison.OrdinalIgnoreCase)))
                        {
                            if (string.IsNullOrEmpty(tmpl.Defaults.Controller) && string.IsNullOrEmpty(tmpl.Defaults.Action))
                            {
                                Ajax.Dna().Button("btnEdit_" + tmpl.Name)
                                    .LocText("RES_EDIT")
                                    .Click("location=\"" + Url.Action("EditWidget", new { Area = "", wid = Server.UrlEncode(tmpl.ID) }) + "\";")
                                    .ImageIcons(Url.Content("~/content/images/design_content.gif"))
                                    .Render();
                            }
                        }
                    </div>
                </td>
            </tr>
        </table>
        <hr />
    </div>
}
@{Ajax.Dna().Pager("wtPager").Load(Url.Action("WidgetManager")).Render();}
<script type="text/javascript">
    function _reloadCurrentWidgetPath(){
        location="@(Url.Action("WidgetManager", "Widget") + "?category=" + _category)";
    }

    function _deleteWidgetDescriptor(_id) {
        $dna.confirm("@Html.Global("WidgetMan_Delete_DlgTitle")", "@Html.Global("WidgetMan_Delete_Msg")", function () {
            $.post('@Url.Action("DeletePackage", "Widget", new { Area = "" })', { wid:_id }, function () {
                _reloadCurrentWidgetPath();
            });
        })
    }

    function _registerWidgetDescriptor(_id) {
        $.post('@Url.Action("Register", "Widget", new { Area = "" })', { wid:_id }, function () {
            _reloadCurrentWidgetPath();
        });
    }

    function _unRegisterWidgetDescriptor(_id) {
        $.post('@Url.Action("Unregister", "Widget", new { Area = "" })', { wid:_id }, function () {
            _reloadCurrentWidgetPath();
        });
    }

    function _detectWidgets(){
      $.post("@Url.Action("Detect", "Widget", new { Area = "" })",function(){  _reloadCurrentWidgetPath(); });
    }
</script>
@Html.RenderScripts()
